<?php
namespace App\Http\Controllers\User;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Session\Store;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\DB;
use App\Models\CommentModel;
use App\Models\GoodsModel;

class CommentController extends Controller{

    public function __construct()
    {
        $this->middleware('user_login');
    }

    //获取评论
    public function comment(Request $request,CommentModel $commentModel,GoodsModel $goodsModel){

        $goods = $goodsModel->where('id',$request->input('goods_id'))->get();
        if(empty($goods)){
            return returnMsg(400,'商品不存在');
        }

        $data = $commentModel->where('goods_id',$request->input('goods_id'))->paginate(15);
        return returnMsg(0,'获取成功',$data);

    }

    //获取评论
    public function add_comment(Request $request,CommentModel $commentModel,GoodsModel $goodsModel){

        $goods = $goodsModel->where('id',$request->input('goods_id'))->get();
        if(empty($goods)){
            return returnMsg(400,'商品不存在');
        }

        $commentModel->comment=$request->input('comment');
        $commentModel->user_id=auth('user_api')->user()->id;
        $commentModel->nickName=auth('user_api')->user()->nickName;
        $commentModel->head_img=auth('user_api')->user()->getOriginal('head_img');
        $commentModel->goods_id=$request->input('goods_id');

        $res = $commentModel->save();
        if(!$res){
            return returnMsg(400,'保存失败');
        }
        return returnMsg(0,'保存成功');

    }

    //删除评论
    public function del_comment(Request $request,CommentModel $commentModel){

      //  $goods = $goodsModel->where('id',$request->input('goods_id'))->get();
        $res = $commentModel->where('user_id',auth('user_api')->user()->id)->where('id',$request->input('comment_id'))->delete();
        if(!$res){
            return returnMsg(400,'修改失败');
        }
        return returnMsg(0,'修改成功');

    }

}